﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using InstantNaukriDAL;
using System.Collections.Generic;

public partial class SuperAdminMaster : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                clsUserLoginDAL objclsUserLoginDAL = new clsUserLoginDAL();

                FillSuperAdmin();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }



    protected void btnsaveSuperAdmininfo_Click(object sender, EventArgs e)
    {


        try
        {
            if (Session["SuperAdminId"] == null)
            {

                clsUserLoginDAL objclsUserLoginDAL = new clsUserLoginDAL();
                //Saving User
                IN_TB_USER objIN_TB_USER = new IN_TB_USER();
                objIN_TB_USER.EMAIL = txtemail.Text;

                if (rbtnLoginId.SelectedValue == "1")
                {
                    objIN_TB_USER.LOGIN_ID = txtemail.Text;
                    objIN_TB_USER.PASSWORD = txtPassword.Text;
                }
                if (rbtnLoginId.SelectedValue == "2")
                {
                    objIN_TB_USER.LOGIN_ID = txtmobile.Text;
                    objIN_TB_USER.PASSWORD = txtPassword.Text;
                }
                objIN_TB_USER.NAME = txtSubAdminName.Text;
                objIN_TB_USER.MOBILE = txtmobile.Text;
                objIN_TB_USER.EMAIL = txtemail.Text;
                objIN_TB_USER.STATUS = 1;
                objIN_TB_USER.ROLEID = 1;
                objIN_TB_USER.DOB = Convert.ToDateTime(txtDob.Text);
                objIN_TB_USER.CREATED_BY = 1;
                int UserId = objclsUserLoginDAL.SaveUser(objIN_TB_USER);


                FillSuperAdmin();


                // FillSubBranch();
            }
            else
            {
                clsUserLoginDAL objclsUserLoginDAL = new clsUserLoginDAL();
                //Saving User
                IN_TB_USER objIN_TB_USER = new IN_TB_USER();
                if (rbtnLoginId.SelectedValue == "1")
                {
                    objIN_TB_USER.LOGIN_ID = txtemail.Text;
                    objIN_TB_USER.PASSWORD = txtPassword.Text;
                }
                if (rbtnLoginId.SelectedValue == "2")
                {
                    objIN_TB_USER.LOGIN_ID = txtmobile.Text;
                    objIN_TB_USER.PASSWORD = txtPassword.Text;
                }

                objIN_TB_USER.ID = Convert.ToInt32(Session["SuperAdminId"].ToString());
                objIN_TB_USER.NAME = txtSubAdminName.Text;
                objIN_TB_USER.MOBILE = txtmobile.Text;
                objIN_TB_USER.EMAIL = txtemail.Text;
                objIN_TB_USER.STATUS = 1;
                objIN_TB_USER.ROLEID = 1;
                objIN_TB_USER.DOB = Convert.ToDateTime(txtDob.Text);
                objIN_TB_USER.CREATED_BY = 1;
                int UserId = objclsUserLoginDAL.SaveUser(objIN_TB_USER);
                FillSuperAdmin();
                // FillSubBranch();
                // ClearAllControls();
                Session["SuperAdminId"] = null;
            }
        }

        catch (Exception)
        {
            throw;
        }
    }



    protected void FillSuperAdmin()
    {
        try
        {
            List<IN_TB_USER> objIN_TB_USERist = new List<IN_TB_USER>();
            clsUserLoginDAL objclsUserLoginDAL = new clsUserLoginDAL();

            // IN_TB_USER objIN_TB_USER = new IN_TB_USER();


            //objIN_TB_EMPLOYER_MSTist = objclsEmployeDAL.GetAllEmployerFullInfo();
            objIN_TB_USERist = objclsUserLoginDAL.GetSuperAdminByRoleID(1);
            if (objIN_TB_USERist != null)
            {
                grdSuperAdmin.DataSource = objIN_TB_USERist;
                grdSuperAdmin.DataBind();
            }
        }
        catch (Exception)
        {

            throw;
        }
    }
    protected void grdSuperAdmin_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Edt")
            {
                int SuperAdminID = Convert.ToInt32(e.CommandArgument.ToString());
                clsUserLoginDAL objclsUserLoginDAL = new clsUserLoginDAL();

                IN_TB_USER objIN_TB_USER = new IN_TB_USER();

                objIN_TB_USER = objclsUserLoginDAL.GetUserByID(SuperAdminID);
                if (objIN_TB_USER != null)
                {
                    //Convert.ToInt32(Session["SuperAdminId"].ToString());
                    txtSubAdminName.Text = objIN_TB_USER.NAME;
                    txtmobile.Text = objIN_TB_USER.MOBILE;
                    txtemail.Text = objIN_TB_USER.EMAIL;
                    txtDob.Text = Convert.ToString(objIN_TB_USER.DOB);
                    Session["SuperAdminId"] = SuperAdminID;

                    FillSuperAdmin();
                }
            }

            if (e.CommandName == "Del")
            {
                int SuperAdminID = Convert.ToInt32(e.CommandArgument.ToString());
                clsUserLoginDAL objclsUserLoginDAL = new clsUserLoginDAL();

                IN_TB_USER objIN_TB_USER = new IN_TB_USER();
                objclsUserLoginDAL.DeleteUser(SuperAdminID);
                FillSuperAdmin();

            }
        }
        catch (Exception)
        {

            throw;
        }



    }
    protected void grdSuperAdmin_RowDataBound(object sender, GridViewRowEventArgs e)
    {


        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Getting State Names
                int RoleId = Convert.ToInt32(e.Row.Cells[5].Text);
                clsRoleDAL objclsRoleDAL = new clsRoleDAL();
                IN_TB_ROLE objIN_TB_ROLE = new IN_TB_ROLE();
                objIN_TB_ROLE = objclsRoleDAL.GetRoleById(RoleId);
                e.Row.Cells[5].Text = objIN_TB_ROLE.ROLE;

            }
        }
        catch (Exception)
        {

            throw;
        }
    }
}
